﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Baddies.Map;
using Baddies.Utils;
using Microsoft.Xna.Framework;

namespace BaddiesEditor.PaintTools
{
    /// <summary>
    /// A one click paint brush for paining tiles on the map.
    /// </summary>
    public class BrushPaintTool : PaintTool
    {
        /// <summary>
        /// Initializes a new instance of the BrushPaintTool class.
        /// </summary>
        /// <param name="cursorIcon">Bitmap image for this tool.</param>
        public BrushPaintTool(System.Drawing.Bitmap cursorIcon)
            : base(cursorIcon)
        {            
        }

        /// <summary>
        /// Applies the tool changes to the map.
        /// </summary>
        /// <param name="map">MapGrid to apply the changes on.</param>
        /// <param name="tileSize">Size of a tile in pixels.</param>
        public override void Use(Baddies.Map.MapGrid map, int tileSize)
        {
            base.Use(map, tileSize);

            // if sX, sY are inside the map
            Vector3 mapPos = map.DrawPosition;

            // The -1 is there to correct the fact that if the user clicks in 
            // the very VERY edge of the map, we don't try to access a non-existing tile (N + 1).
            Rectangle mapRect = new Rectangle(
                (int)mapPos.X, 
                (int)mapPos.Y, 
                (map.MapWidth * map.TileSize) - 1, 
                (map.MapHeight * map.TileSize) - 1);

            if (Util.PoinInRect(mapRect, (int)this.ScreenPosition.X, (int)this.ScreenPosition.Y))
            {
                // find the tile on dmX+sX, dmY+sY (where dm = map draw coords)
                MapTile tile = map.GetTileFromRealPos(
                    -(int)mapPos.X + (int)this.ScreenPosition.X, 
                    -(int)mapPos.Y + (int)this.ScreenPosition.Y);

                // set it to the specified tile texture
                tile.SetDrawTexture(
                    new Rectangle(
                        (int)this.SelectedTileImage.X, 
                        (int)this.SelectedTileImage.Y, 
                        tileSize, 
                        tileSize));
            }
        }
    }
}
